//
//  PKReadViewController.m
//  PianKe
//
//  Created by lanou on 15/9/25.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKReadViewController.h"
#import "PKRequestManager.h"
#import "PKReadMainListModel.h"
#import "PKReadMainCarouselModel.h"
#import "PKReadMainListCarouseView.h"
#import "PKReadMainPageCollectionViewController.h"
#import "PKArticleDetailViewController.h"
@interface PKReadViewController ()

@property(nonatomic,strong)NSMutableArray *carouselArr;
@property(nonatomic,strong)NSMutableArray *listArr;
@property(nonatomic,strong)UIScrollView *mainScrollerView;
@property(nonatomic,strong)PKReadMainListCarouseView *readMainCarouseView;
@property(nonatomic,strong) PKReadMainPageCollectionViewController *reaMainListCVC;
@property(nonatomic,strong)UIView *footView;
@end

@implementation PKReadViewController
-(NSMutableArray *)carouselArr
{
    if (_carouselArr==nil) {
        _carouselArr=[NSMutableArray arrayWithCapacity:0];
    }
    return _carouselArr;
}
-(NSMutableArray *)listArr
{
    if (_listArr==nil) {
        _listArr=[NSMutableArray arrayWithCapacity:0];
    }
    return _listArr;
}
-(void)acquireData
{
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    dict[@"client"]=@"1";
    
    dict[@"deviceid"]=@"63A94D37-33F9-40FF-9EBB-481182338873";
    dict[@"auth"]=@"BsdsQiiw9K9DCMVzGMTikDJxQ8auML6E82dsXj1lcWP23tBPTxRY4Pw";
    dict[@"version"]=@"3.0.2";
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/read/columns" patDic:dict finish:^(NSData *data) {
        NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        
#warning 解析轮播图的数组
        NSMutableArray *CarouseArr=dict[@"data"][@"carousel"];
        for (NSDictionary *carouseDict in  CarouseArr) {
            PKReadMainCarouselModel *carouseModel=[[PKReadMainCarouselModel alloc]init];
            [carouseModel  setValuesForKeysWithDictionary:carouseDict];
            [self.carouselArr addObject:carouseModel];
        }
        _readMainCarouseView.CarouseArray=self.carouselArr;
        
#warning 解析主界面的ListArr的数据
        NSMutableArray *listArray=dict[@"data"][@"list"];
        for (NSDictionary *listDict in listArray) {
            PKReadMainListModel *readMainlistModel=[[PKReadMainListModel alloc]init];
            [readMainlistModel setValuesForKeysWithDictionary:listDict];
            [self.listArr addObject:readMainlistModel];
        }
        self.reaMainListCVC.readMainListArr=self.listArr;
        [self.reaMainListCVC.collectionView reloadData];
        NSLog(@"%@",self.reaMainListCVC.readMainListArr);
        NSLog(@"阅读主页面的数据的listArr数据%@",self.listArr);
        NSLog(@"阅读主页面的数据轮播图数据%@",self.carouselArr);
    } Error:^(NSError *error) {
        NSLog(@"阅读主页面的数据解析失败");
    }];
    
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text=@"分组";
    self.view.backgroundColor=[UIColor whiteColor];
    // Do any additional setup after loading the view.
    [self acquireData];
    _mainScrollerView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-70)];
    _mainScrollerView.contentSize=CGSizeMake(0, SCREEN_HEIGHT+20);
    _readMainCarouseView=[[PKReadMainListCarouseView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 120)];
    __block PKReadViewController *test=self;
    _readMainCarouseView.tapCarouselBlock=^(NSInteger count){
        NSString *url=((PKReadMainCarouselModel *)test.carouselArr[count]).url;
        NSString *contentid=[[url componentsSeparatedByString:@"/"]lastObject];
        NSLog(@"contentid%@",contentid);
        PKArticleDetailViewController  *articleDetailVC=[[PKArticleDetailViewController  alloc]init];
        articleDetailVC.contentid=contentid;
        [test.navigationController pushViewController:articleDetailVC animated:YES];
    
    };
#warning 数据解析是在block中解析的所以在创建视图的时候数据有出来，所必须在数据解析完之后才能够进行赋值
    //    _readMainCarouseView.CarouseArray=self.carouselArr;
    [_mainScrollerView addSubview:_readMainCarouseView];
    UICollectionViewFlowLayout *flowLayout=[[UICollectionViewFlowLayout alloc]init];
  self.reaMainListCVC =[[PKReadMainPageCollectionViewController alloc]initWithCollectionViewLayout:flowLayout];
    self.reaMainListCVC.collectionView.frame=CGRectMake(10, 130, SCREEN_WIDTH-20, SCREEN_HEIGHT-140);
    self.reaMainListCVC.collectionView.backgroundColor=[UIColor whiteColor];    
    [_mainScrollerView addSubview:self.reaMainListCVC.collectionView];
    
    self.footView =[[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-135,SCREEN_WIDTH , 150)];
    self.footView.backgroundColor=[UIColor greenColor];
    [_mainScrollerView   addSubview:self.footView];

    [self.view addSubview:_mainScrollerView ];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
